package com.qf.qfwemedia.service.impl;


import com.alibaba.fastjson.JSON;
import com.qf.qfleadnewsfeignapi.schedule.ScheduleApi;
import com.qf.qfleadnewsmodel.commons.dtos.ResponseResult;
import com.qf.qfleadnewsmodel.enums.TaskTypeEnum;
import com.qf.qfleadnewsmodel.schedule.pojos.Taskinfo;
import com.qf.qfleadnewsmodel.wemedia.pojos.WmNews;
import com.qf.qfleadnewsutils.commons.ProtostuffUtil;
import com.qf.qfwemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;

@Service
@Slf4j
public class WmNewsDelayPubServiceImpl {

    @Autowired
    private ScheduleApi scheduleApi;

    @Autowired
    private WmNewsService wmNewsService;

    @Scheduled(cron = "0/2 * * * * ? ")
    public void pub() throws UnsupportedEncodingException {
        log.info("任务发布检查定时执行.......");

        ResponseResult result = scheduleApi.consumeTask(TaskTypeEnum.NEWS_SCAN_TIME.getTaskType(), TaskTypeEnum.NEWS_SCAN_TIME.getPriority());

        if (result.getCode() == 200) {
            //是我们任务调度服务中返回的数据-TaskInfo.进行序列化传输过程中，泛型丢失
            String taskInfoJson = JSON.toJSONString(result.getData());
            Taskinfo taskinfo = JSON.parseObject(taskInfoJson, Taskinfo.class);

            log.info("任务发布-获取调度服务器中的任务，任务数据：{}.......",taskinfo);

            byte[] parameters = taskinfo.getParameters();
            //将byte[]转成具体的参数值
            String newsId = ProtostuffUtil.deserialize(parameters, String.class);

            WmNews wmNews = wmNewsService.getById(newsId);

            if (wmNews != null){
                wmNewsService.autoScan(wmNews);
            }
        }

    }
}
